layui.use(['table','laytpl','layer', 'form', 'layedit', 'laydate'], function() {
	var table = layui.table;
	var laytpl = layui.laytpl;
	var laydate = layui.laydate;
	table.render({
		elem : '#user-table',
		url : '/admin/userdic/pageQuery.json',
		id : 'mytable',
		page : true,
		height : 'full-165',
		limits: [90,150,300],
		limit: 150,
		request: {
		  pageName: 'currentPageNo' //页码的参数名称，默认：page
		  ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
		},
		cols : [ [
			{
				checkbox : true
			}
			, {
				field : 'dicId',
				title : '序号',
				sort: true
			}
			, {
				field : 'name',
				title : '用户词',
				sort: true
			}
			, {
				field : 'nature',
				title : '词性',
				sort: true
			}
			, {
				field : 'freq',
				title : '词频',
				sort: true
			}
			, {
				field : 'type',
				title : '用户词类型',
				sort: true
			}
			, {
				field : 'rowUpdateTime',
				title : '更新时间',
				sort: true
			}
			, {
				field : 'rowUpdateTime',
				fixed : 'right',
				align : 'center',
				sort: true,
				toolbar : '#optbar',
				width : 200
			}
		] ]
	});
	table.on('tool', function(obj){
	    var data = obj.data;
	    if(obj.event === 'del'){
	      layer.confirm('确定删除'+obj.data.name+'吗？', function(index){
    	  $.ajax({
    		    type: 'POST',
    		    url: '/admin/userdic/deleteUserdicByPrimaryKey.json',
    		    data: {dicId:obj.data.dicId},
    		    success: function (data) {
    		        if(data.success){
    		        	obj.del();
    			        layer.close(index);
    		        }else{
    		        	layer.msg(data.message,{title:'错误',icon: 5,anim: 6,time: 0,btn:['确定']});
    		        }
    		    },
    		    error: function(data) {
    		    	layer.msg(data.responseText,{title:'错误',icon: 5,anim: 6,time: 0,btn:['确定']});
    		    },
    		    dataType: "json"
    		})
	      });
	    } else if(obj.event === 'edit'){
	    	openWindow('edit',data.dicId);
	    }
	  });


	var $ = layui.$,
		active = {
			reload : function() {
				table.reload('mytable', {
					where : {
						name : $('#name').val(),
						type : $('#type').val()
					}
				});
			}
		};
	$('#searchButton').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	$('#addUser').click(function(){
		openWindow('new');
	});
	$('#refresh').click(function(){
		table.reload('mytable');
	});
	//------------------------------------------------
	function openWindow(optname,dicId) {
		var optstr = '新建userdic';
		if(optname == 'edit'){
			optstr = '编辑userdic';
		}
		var mylayer = layer.open({
			type : 1,
			title : optstr,
			skin : 'layui-layer-lan',
			anim : 5,
			shadeClose : false,
			offset: [($(window).height()/8), $(window).width()/4],
			content : '<div id="standardForm"></div>',
			btn : [ '提交', '取消' ],
			closeBtn : false,
			btn1 : function(index, layero) {
				$('#mybutton').click();
			},
			btn2 : function(index, layero) {
				layer.close(mylayer);
			}
		});
		var getTpl = formftl.innerHTML,
			view = document.getElementById('standardForm');
		laytpl(getTpl).render({}, function(html) {
			view.innerHTML = html;
		});
		var form = layui.form;
		form.render();
		
		laydate.render({ 
			  elem: '#win-rowUpdateTime'
			  ,type: 'datetime'
			});
		
		if(optname == 'edit'){
			$('#optname').val('edit');
			$('input[name="passWord"]').removeAttr('lay-verify');
		}else{
			$('#optname').val('new');
		}
		form.verify({
			pass : function(value) {
				if (value != $('input[name="passWord"]').val()) {
					return '两次密码不一致';
				}
			}
		});
		if(optname == 'edit'){
			$.ajax({
			    type: 'POST',
			    url: '/admin/userdic/loadUserdicByPrimaryKey.json',
			    data: {dicId:dicId},
			    success: function (data) {
			        if(data.success){
			        	$('#win-dicId').val(data.info.dicId);
			        	$('#win-dicId').val(data.info.dicId);
			        	$('#win-name').val(data.info.name);
			        	$('#win-nature').val(data.info.nature);
			        	$('#win-freq').val(data.info.freq);
			        	$('#win-type').val(data.info.type);
			        	$('#win-rowUpdateTime').val(data.info.rowUpdateTime);
			        	form.render('select');
			        }else{
			        	layer.msg(data.message,{icon: 5,anim: 6,time: 2000});
			        }
			    },
			    error: function(data) {
			    	layer.msg(data.responseText,{icon: 5,anim: 6,time: 2000});
			    },
			    dataType: "json"
			})
		}
		
		
		/**
		 * 表单验证通过后在这里提交表单
		 */
		form.on('submit(mybutton)', function(data) {
			if(optname == 'edit'){
				$.ajax({
				    type: 'POST',
				    url: '/admin/userdic/updateUserDicByPrimaryKey.json',
				    data: data.field,
				    success: function (data) {
				    	
				        if(data.success){
				        	layer.msg(data.message);
				        	layer.close(mylayer);
				        	table.reload('mytable');
				        }else{
				        	layer.msg(data.message,{icon: 5,anim: 6,time: 2000});
				        }
				    },
				    error: function(data) {
				    	layer.msg(data.responseText,{icon: 5,anim: 6,time: 2000});
				    },
				    dataType: "json"
				})
			} else {
				$.ajax({
				    type: 'POST',
				    url: '/admin/userdic/addUserDic.json',
				    data: data.field,
				    success: function (data) {
				        if(data.success){
				        	layer.msg(data.message);
				        	layer.close(mylayer);
				        	table.reload('mytable');
				        }else{
				        	layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
				        }
				    },
				    error: function(data) {
				    	layer.msg(data.responseText,{icon: 5,anim: 6,time:0,btn:['确定']});
				    },
				    dataType: "json"
				})
			}
			return false;
		});
	}
	//------------------------------------------------
	$.ajax({
	    type: 'POST',
	    url: '/admin/userdic/getAllDicType.json',
	    success: function (data) {
	        if(data.success){
	        	var htmltmp = '';
	        	$.each(data.list,function(i,item){
	        		htmltmp += '<option value="'+item.code+'">'+item.msg+'</option>';
	        	});
	        	$('#type').html(htmltmp);
	        	var form = layui.form;
	        	form.render();
	        }else{
	        	layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
	        }
	    },
	    error: function(data) {
	    	layer.msg(data.responseText,{icon: 5,anim: 6,time: 2000});
	    },
	    dataType: "json"
	})
});